package com.wyl.wyl.testapplication.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wyl.wyl.testapplication.Api;
import com.wyl.wyl.testapplication.R;
import com.wyl.wyl.testapplication.base.BaseActivity;
import com.wyl.wyl.testapplication.bean.CarMainBean;
import com.wyl.wyl.testapplication.utils.DialogUtil;
import com.wyl.wyl.testapplication.utils.SP.SPUtils;
import com.wyl.wyl.testapplication.constant.UrlConstant;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ReserveActivity extends BaseActivity {

    private Activity mActivity;
    private List<CarMainBean.BodyBean.WhListBean> carList;
    private TextView choice, next;
    private String whId,wh_name;
    private LinearLayout back;
    private ProgressDialog dialog;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.activity_reserve);
        mActivity = this;
        initView();
        initData();
    }


    private void initData() {

        if (dialog != null) {
            dialog.setMessage("正在加载数据...");
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }

        getData();

    }

    private void getData () {
//        Retrofit retrofit = new Retrofit.Builder().baseUrl(UrlConstant.DEFAULT_HTTP + SPUtils.getDomain(ReserveActivity.this) + ":" + SPUtils.getPort(ReserveActivity.this)).
//                addConverterFactory(GsonConverterFactory.create()).build();
//        Log.i("aaa", UrlConstant.DEFAULT_HTTP + SPUtils.getDomain(ReserveActivity.this) + ":" + SPUtils.getPort(ReserveActivity.this));
//        retrofit.create(Api.class).getWhList().enqueue(new Callback<CarMainBean>() {
//            @Override
//            public void onResponse(Call<CarMainBean> call, Response<CarMainBean> response) {
//                if (response.body().getHead().getStatus().equals("true")) {
//                    Gson gson = new Gson();
//                    Log.i("aaa",gson.toJson(response.body().getBody() , CarMainBean.BodyBean.class));
//                    carList = response.body().getBody().getWh_list();
//                    dialog.dismiss();
//                    DialogUtil.getLocationDialog(mActivity, carList, new DialogUtil.LocationListener() {
//                        @Override
//                        public void onSelect(Dialog dialog, String code, String name, String id) {
//                            choice.setText(code + "-" + name);
//                            whId = id;
//                            wh_name=name;
//                        }
//                    }).show();
//                }else {
//                    Toast.makeText(mActivity, "请求失败", Toast.LENGTH_SHORT).show();
//                }
//            }
//
//            @Override
//            public void onFailure(Call<CarMainBean> call, Throwable t) {
//                dialog.dismiss();
//                Toast.makeText(mActivity, "请求失败", Toast.LENGTH_SHORT).show();
//            }
//        });
    }
    private void initView() {
//        title = (TextView) findViewById(R.id.headerTitle);
//        title.setText("创建转储管理");

        dialog = new ProgressDialog(ReserveActivity.this);
        choice = findViewById(R.id.choice);
        next = findViewById(R.id.next);
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        choice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (carList == null || carList.isEmpty()) {
                    getData();
                }else {
                    DialogUtil.getLocationDialog(mActivity, carList, new DialogUtil.LocationListener() {
                        @Override
                        public void onSelect(Dialog dialog, String code, String name, String id) {
                            choice.setText(code + "-" + name);
                            whId = id;
                            wh_name=name;
                        }
                    }).show();
                }
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(whId)) {
                    Toast.makeText(mActivity, "请选择仓库号", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(ReserveActivity.this, PersonActivity.class);
                intent.putExtra("whId", whId);
                intent.putExtra("wh_name",wh_name);
                startActivity(intent);
            }
        });


    }

}
